package com.qire.manhua.dialog;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

import com.qire.antsrouter.AntsRouter;
import com.qire.antsrouter.card.FloorCard;
import com.qire.common.basic.ObjectUtil;
import com.qire.common.constant.CacheStorageKey;
import com.qire.common.constant.DeviceConfigInfo;
import com.qire.common.constant.Navigation;
import com.qire.common.constant.Url;
import com.qire.common.function.Action;
import com.qire.common.support.base.BaseDialog;
import com.qire.common.utils.CacheStorageUtil;
import com.qire.manhua.databinding.DialogUserAgreementBinding;
import com.qire.manhua.view.animator.CheckAnimationBinder;

import androidx.annotation.NonNull;

/**
 * 用户协议弹窗（政策要求的弹窗）：只在首次使用APP时弹出提示。
 */
public class UserAgreementDialog extends BaseDialog {

    /** @return true 以同意用户协议内容   false 未同意用户协议内容 */
    public static boolean checkUserAgreementState() {
        return CacheStorageUtil.queryBoolean(CacheStorageKey.USER_AGREEMENT_STATE, false);
    }


    /** 勾选动画 */
    public final CheckAnimationBinder checkAnimation = new CheckAnimationBinder(this::agreeAnimationCallback);

    /** 视图数据绑定器 */
    private final DialogUserAgreementBinding viewDataBinder;

    /** 同意的处理行为， 不同意的处理行为 */
    private Action agreeAction, disagreeAction;

    public UserAgreementDialog(@NonNull Context context) {
        super(context);
        viewDataBinder = DialogUserAgreementBinding.inflate(LayoutInflater.from(context), null, false);
        viewDataBinder.setUserAgreementDialog(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(viewDataBinder.getRoot());
        setCancelable(false);
    }

    public void setAgreeAction(Action agreeAction) {
        this.agreeAction = agreeAction;
    }

    public void setDisagreeAction(Action disagreeAction) {
        this.disagreeAction = disagreeAction;
    }

    private void agreeAnimationCallback() {
        dismiss();
        if(ObjectUtil.nonNull(agreeAction)) {
            agreeAction.call();
            DeviceConfigInfo.saveServiceProtocolVer();
            DeviceConfigInfo.savePrivacyProtocolVer();
        }
    }

    /**
     * 同意单击事件
     * @param view
     */
    public void agreeBtnOnClick(View view) {
        CacheStorageUtil.save(CacheStorageKey.USER_AGREEMENT_STATE, true);
        checkAnimation.goPlay();
    }

    /**
     * 不同意单击事件
     * @param view
     */
    public void disagreeBtnOnClick(View view) {
        if (ObjectUtil.nonNull(disagreeAction)) {
            disagreeAction.call();
        }
    }

    public void serviceTermsOnClick(View view) {
        checkAnimation.onceDisableDestroy();
        AntsRouter.ROUTER.<FloorCard>buildCard(Navigation.Page.WebPage)
                .withData("title","服务条款")
                .withData("url", Url.serviceTerms_url)
                .navigation();
    }

    public void privacyPolicyOnClick(View view) {
        checkAnimation.onceDisableDestroy();
        AntsRouter.ROUTER.<FloorCard>buildCard(Navigation.Page.WebPage)
                .withData("title","隐私条款")
                .withData("url", Url.privacyPolicy_url)
                .navigation();
    }

}
